﻿using CJAtech.BBS.Business;
using CJAtech.BBS.DataEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bigger.Framework;
using Bigger.Web.Mvc;
using Bigger.Base.Cache;
using System.Web.Mvc;
using Bigger.Web.Helper;

namespace CJAtech.BBS.Process
{
    public class BBSConfigFilter : BiggerActionFilter
    {
        public string BBSName { get; private set; }
        public BBSConfigFilter(string bbsName)
        {
            this.BBSName = bbsName;
        }
        public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
        {
            if (this.BBSName.IsNull())
            {
                filterContext.Result = new Http404Result();
                return;
            }
            BizCommunityConfig objBizCommunityConfig = new BizCommunityConfig();
            CommunityConfig objCommunityConfig = objBizCommunityConfig.GetRecordCache("", TimeSpan.FromSeconds(10), "ConfigCode=@ConfigCode", new { ConfigCode = this.BBSName });
            if (objCommunityConfig == null)
            {
                if (SystemWebAsset.IsAjaxRequest() || SystemWebAsset.IsCrossDomain())
                {
                    filterContext.Result = new JsonResult
                    {
                        Data = new InvokeResult { ResultCode = "BBSNameNull", ResultMessage = "对不起，您请求的地址不存在" },
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };
                }
                else
                {
                    filterContext.Result = new Http404Result();
                }
                return;
            }

            filterContext.HttpContext.SetItems("CommunityConfig", objCommunityConfig);
            //filterContext.Controller.ViewBag.CommunityConfig = objCommunityConfig;
        }
    }
}
